﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommonTool;
namespace AutoMachine
{
    public partial class Frm_Debug : Form
    {
        public Frm_Debug()
        {
            InitializeComponent();
        }

        #region 窗体实例化
        /// <summary>
        /// 窗体对象实例化
        /// </summary>
        private static Frm_Debug _instance;
        internal static Frm_Debug Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Frm_Debug();
                return _instance;
            }
        }
        #endregion

        private void Frm_Debug_FormClosing(object sender, FormClosingEventArgs e)
        {
            Frm_Debug._instance = null;
        }

        private void Frm_Debug_Load(object sender, EventArgs e)
        {
            Array ar = Enum.GetValues(typeof(SysBit));
            dgvBit.Rows.Add(ar.Length);
            UpdateSysBit();

            Array ar1 = Enum.GetValues(typeof(SysInt));
            dgvInt.Rows.Add(ar1.Length);
            UpdateSysInt();

            if (Device.OperMode == Device.OperationMode.NormalMode)
            {
                this.comboBox1.SelectedIndex = 0;
            }
            if (Device.OperMode == Device.OperationMode.DryRunMode)
            {
                this.comboBox1.SelectedIndex = 1;
            }

            if (GlobalVar.Buzzer == true)
            {
                this.checkBox1.Checked = true;
            }
            else
            {
                this.checkBox1.Checked = false;
            }

            if (GlobalVar.OneStep == true)
            {
                this.checkBox2.Checked = true;
            }
            else
            {
                this.checkBox2.Checked = false;
            }
            if (GlobalVar.VisScreen == true)
            {
                this.checkBox3.Checked = true;
            }
            else
            {
                this.checkBox3.Checked = false;
            }

            if (GlobalVar.StandLine == true)
            {
                this.checkBox4.Checked = true;
            }
            else
            {
                this.checkBox4.Checked = false;
            }

            if (GlobalVar.VisRepate == true)
            {
                this.checkBox5.Checked = true;
            }
            else
            {
                this.checkBox5.Checked = false;
            }

            if (GlobalVar.Door == true)
            {
                this.checkBox6.Checked = true;
            }
            else
            {
                this.checkBox6.Checked = false;
            }

        }

        private void UpdateSysBit()
        {
            int i = 0;
            Array ar = Enum.GetValues(typeof(SysBit));
            foreach (SysBit sbr in ar)
            {
                dgvBit.Rows[i].Cells[0].Value = ((int)(sbr)).ToString();
                dgvBit.Rows[i].Cells[1].Value = sbr.ToString();  
                i++;
            }
        }

        private void UpdateSysInt()
        {
            int i = 0;
            Array ar = Enum.GetValues(typeof(SysInt));
            foreach (SysInt sbr in ar)
            {
                dgvInt.Rows[i].Cells[0].Value = ((int)(sbr)).ToString();
                dgvInt.Rows[i].Cells[1].Value = sbr.ToString();
                i++;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Array ar = Enum.GetValues(typeof(SysBit));
            Array ar1 = Enum.GetValues(typeof(SysInt));
            //更新
            for (int i = 0; i < ar.Length; i++)
            {
                if (SystemMgr.GetInstance().GetSysBit((SysBit)i) == true)//输入负限位
                {
                    Tool.CtrDisplay.ShowText(this.dgvBit, i, 2, "True", Color.Green, Color.Empty);//显示状态                
                }
                else
                {
                    Tool.CtrDisplay.ShowText(this.dgvBit, i, 2, "False", Color.White, Color.Empty);
                }
                this.dgvBit.Rows[i].Cells[3].Value = SystemMgr.GetInstance().GetSysBit((SysBit)i);
                this.dgvBit.Rows[i].Cells[4].Value = !SystemMgr.GetInstance().GetSysBit((SysBit)i);
            }

            for (int i = 0; i < ar1.Length; i++)
            {
                Tool.CtrDisplay.ShowText(this.dgvInt, i, 2, SystemMgr.GetInstance().GetSysInt(i).ToString(), Color.Empty, Color.Empty);
            } 
        }

        private void dgvBit_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                Array ar = Enum.GetValues(typeof(SysBit));
                SystemMgr.GetInstance().WriteSysBit((SysBit)ar.GetValue(e.RowIndex), true);
            }
            if (e.ColumnIndex == 4)
            {
                Array ar = Enum.GetValues(typeof(SysBit));
                SystemMgr.GetInstance().WriteSysBit((SysBit)ar.GetValue(e.RowIndex), false);
            }

        }

        /// <summary>
        /// 流程变量置0
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvInt_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dgvInt.SelectedCells[0].RowIndex;//行号 轴号

            int col = dgvInt.SelectedCells[0].ColumnIndex;// 列号   

            if (e.ColumnIndex == 3)
            {
                Array ar = Enum.GetValues(typeof(SysInt));

                SystemMgr.GetInstance().WriteSysInt((SysInt)ar.GetValue(e.RowIndex), 0);
            }
        }

        private void Frm_Debug_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            Frm_Debug._instance = null; ;
        }

        /// <summary>
        /// 空跑模式切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = this.comboBox1.Text.Trim();
            if (name == "正常运行模式")
            {
                Device.ChangeOperationMode(Device.OperationMode.NormalMode);
            }
            if (name == "空跑运行模式")
            {
                Device.ChangeOperationMode(Device.OperationMode.DryRunMode);
            }
        }

        /// <summary>
        /// 蜂鸣器屏蔽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                GlobalVar.BuzzerScreen = true;
               
            }
            else
            {
                GlobalVar.BuzzerScreen = false;
            }
        }

        /// <summary>
        /// 单步模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox2.Checked)
            {
                GlobalVar.OneStep = true;

            }
            else
            {
                GlobalVar.OneStep = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox3.Checked)
            {
                GlobalVar.VisScreen = true;
            }
            else
            {
                GlobalVar.VisScreen = false;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox4.Checked)
            {
                GlobalVar.StandLine = true;
            }
            else
            {
                GlobalVar.StandLine = false;
            }
        }

        /// <summary>
        /// 清除板卡总线错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //清除板卡总线错误
            Motions.MotionMgr.GetInstance().Card.ClearBusAlarm();
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox5.Checked)
            {
                GlobalVar.VisRepate = true;
            }
            else
            {
                GlobalVar.VisRepate = false;
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox6.Checked)
            {
                GlobalVar.Door = true;
            }
            else
            {
                GlobalVar.Door = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GlobalVar.ClearRun = true;
        }
    }
}
